package cn.tedu.blog.server.controller.system;


import cn.tedu.blog.server.pojo.dto.UserAddNewDTO;
import cn.tedu.blog.server.pojo.dto.UserLoginDTO;
import cn.tedu.blog.server.pojo.vo.UserListVO;
import cn.tedu.blog.server.service.IUserService;
import cn.tedu.blog.server.utils.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author 雷茂林
 * @description: 后台系统用户管理控制器
 * @date
 */
@Api(tags = "1. 系统角色管理")
@Slf4j
@RestController
@RequestMapping("/system/users")
public class UserController {

    @Autowired
    private IUserService userService;


    @ApiOperation("创建用户")
    @ApiOperationSupport(order = 100)
    @PostMapping("/createUser")
    @PreAuthorize("hasAuthority('/system/users/createUser')")
    public JsonResult createUser(@RequestBody UserAddNewDTO userAddNewDTO){
        log.debug("接收到客户端提交的请求，参数：{}",userAddNewDTO);
        userService.createUser(userAddNewDTO);
        return JsonResult.ok();
    }



    @ApiOperation("删除用户")
    @ApiOperationSupport(order = 200)
    @PostMapping("/deleteById")
    @PreAuthorize("hasAuthority('/system/users/deleteById')")
    public JsonResult deleteById(@RequestBody UserListVO userListVO){
        log.debug("接收到客户端提交的请求，参数：{}",userListVO.getId());
        userService.deleteById(userListVO.getId());
        return JsonResult.ok();
    }



    @ApiOperation("批量删除用户")
    @ApiOperationSupport(order = 210)
    @PostMapping("/deleteByIds")
    @PreAuthorize("hasAuthority('/system/users/deleteByIds')")
    public JsonResult deleteByIds(@RequestBody List<Long> list){
        log.debug("接收到客户端提交的请求，参数：{}",list);
        userService.deleteByIds(list);
        return JsonResult.ok();
    }



    @ApiOperation("修改用户")
    @ApiOperationSupport(order = 300)
    @PostMapping("/updateById")
    @PreAuthorize("hasAuthority('/system/users/updateById')")
    public JsonResult updateById(@RequestBody UserAddNewDTO userAddNewDTO){
        log.debug("【updateById】接收到客户端提交的请求，参数：{}",userAddNewDTO);
        userService.updateById(userAddNewDTO);
        return JsonResult.ok();
    }



    @ApiOperation("条件分页查询")
    @ApiOperationSupport(order = 400)
    @PostMapping("/listUsersByCondition")
    public List<UserListVO> listUsersByCondition(@RequestBody UserListVO userListVO){
        log.debug("【list】接收到客户端提交的请求，参数：{}",userListVO);
        return userService.listByKeyword(userListVO);
    }



    @PostMapping("/getCountAll")
    public int getCountAll(@RequestBody UserListVO userListVO){
        log.debug("【countAll】接收到客户端提交的请求，参数：{}",userListVO);
        return userService.getCountAll(userListVO);
    }



    @GetMapping("/getBackFillById")
    public UserListVO getBackFillById(Long id){
        log.debug("接收到客户端提交的请求，参数：{}",id);
        return userService.getBackFillById(id);
    }



    @ApiOperation("用户登录")
    @ApiOperationSupport(order = 500)
    @PostMapping("/login")
    public JsonResult login(@RequestBody UserLoginDTO userLoginDTO){
        log.debug("接收到客户端提交的请求，参数：{}",userLoginDTO);
        return JsonResult.ok(userService.login(userLoginDTO));
    }
}
